﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ScaffoldIt.Configuration;

namespace ScaffoldIt
{
    public partial class FormEditPreset : Form
    {
        public FormEditPreset()
        {
            InitializeComponent();
        }

        public static bool EditPreset(Preset preset)
        {
            using (var frm = new FormEditPreset())
            {
                frm.Init(preset);
                frm.textBoxName.Focus();
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    frm.Read(preset);
                    return true;
                }
            }

            return false;
        }

        private void Read(Preset preset)
        {
            preset.Name = textBoxName.Text;
            preset.Description = textBoxDescription.Text;
        }

        private void Init(Preset preset)
        {
            textBoxName.Text = preset.Name;
            textBoxDescription.Text = preset.Description;
        }

        private void textBoxName_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxName.Text))
            {
                e.Cancel = true;
                errorProvider1.SetError(textBoxName, "You must provide a name");
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.ValidateChildren())
                DialogResult = DialogResult.OK;
        }
    }
}
